import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author: gao
 * @create-date: 2025/3/2 12:04
 */
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {}

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public List<Integer> inorderTraversal(TreeNode root){
        List<Integer> tree = new ArrayList<>();

        if(root == null){
            return tree;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.isEmpty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            tree.add(cur.val);
            cur = cur.right;
        }
        return tree;
    }
}
